package PRO;

import java.math.BigInteger;

public class BigMath {

    // Enteros como variables
    public static final BigInteger CERO = new BigInteger("0");
    public static final BigInteger UNO = new BigInteger("1");
    public static final BigInteger DOS = new BigInteger("2");
    public static final BigInteger NEG_UNO = new BigInteger("-1");

    //Metodo que obtiene la raiz cuadrada y de ese valor toma el piso
    public static BigInteger raizPiso(BigInteger n) {
        BigInteger r = BigInteger.ZERO;
        BigInteger m = r.setBit(2 * n.bitLength());
        BigInteger nr;

        do {
            //nr = r + m
            nr = r.add(m);

            //si n > nr entonces
            if (nr.compareTo(n) != 1) {
                n = n.subtract(nr);

                //r = nr + m
                r = nr.add(m);
            }

            //r.shiftRight(1) sera r/2^(1) osea r/2
            r = r.shiftRight(1);

            //m.shiftRight(2) sera m/2^(2) osea m/4
            m = m.shiftRight(2);
        } while (m.bitCount() != 0);
        return r;
    }

}
